package com.jml.zlhq.app.adapter;

import java.util.List;

import org.json.JSONObject;

import android.app.Activity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.jml.zlhq.app.R;
import com.jml.zlhq.app.AppContext;
import com.jml.zlhq.app.util.BaseTools;
import com.jml.zlhq.app.util.StringUtil;
import com.jml.zlhq.app.util.UIUtil;
import com.jml.zlhq.app.widgets.RoundAngleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MainFaXianMoreAdapter extends IBaseAdapter {
	private List<JSONObject> itemList;
	private AppContext appContext;
	@SuppressWarnings("unused")
	private Activity activity;

	public MainFaXianMoreAdapter(Activity activity, List<JSONObject> itemList) {
		this.itemList = itemList;
		this.appContext = (AppContext) activity.getApplication();
		this.activity = activity;
	}

	@Override
	public int getCount() {
		int count = itemList == null ? 0 : itemList.size();
		return count;
	}

	@Override
	public Object getItem(int position) {
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		convertView = createItem(position, convertView, parent);
		return convertView;
	}
	
	static class ItemHolder {
		public RoundAngleImageView image;
		public TextView name,privce,suju,nomore;
		public ImageView vip,zan;
		public RatingBar ratingBar;
	}
	private View createItem(int position, View convertView, final ViewGroup parent){
		ItemHolder holder = null;
		if(convertView==null){
			holder = new ItemHolder();
			int main_faxian_item = R.layout.main_faxian_item;
			convertView = LayoutInflater.from(appContext).inflate(main_faxian_item, null);
			holder.image = (RoundAngleImageView) convertView.findViewById(R.id.main_faxian_item_image);
			holder.name = (TextView) convertView.findViewById(R.id.main_faxian_item_name);
			//holder.privce = (TextView) convertView.findViewById(R.id.main_faxian_item_price);
			holder.suju = (TextView) convertView.findViewById(R.id.main_faxian_item_suju);
			holder.nomore = (TextView) convertView.findViewById(R.id.main_faxian_item_nomore);
			//holder.vip = (ImageView) convertView.findViewById(R.id.main_faxian_item_vip);
			//holder.zan = (ImageView) convertView.findViewById(R.id.main_faxian_item_zan);
			//holder.ratingBar = (RatingBar) convertView.findViewById(R.id.main_faxian_item_ratingbar);
			convertView.setTag(holder);
		}else{
			holder = (ItemHolder) convertView.getTag();
		}
		JSONObject json = itemList.get(position);
		try {
			holder.nomore.setVisibility((position==(itemList.size()-1)&&pageEnd)?View.VISIBLE:View.GONE);
//			Log.i("=====", position+","+(holder.nomore.getVisibility()==View.VISIBLE));
			holder.name.setText(BaseTools.getFrom("name", json));
			ImageLoader.getInstance().displayImage(BaseTools.getFrom("logo_url.image_url", json), holder.image, UIUtil.getImageLoaderDisplayOptions(R.drawable.default140));
			String price = BaseTools.getFrom("average_price", json);
			holder.privce.setText(StringUtil.isEmpty(price)||price.equals("0")?"":"均价"+BaseTools.getFrom("average_price", json));
			holder.suju.setText(Html.fromHtml("浏览&nbsp;&nbsp;"+BaseTools.getFrom("statistic.CLICK", json)+"&nbsp;&nbsp;&nbsp;&nbsp;收藏&nbsp;&nbsp;"+BaseTools.getFrom("statistic.COLLECT", json)+"&nbsp;&nbsp;&nbsp;&nbsp;作品&nbsp;&nbsp;"+BaseTools.getFrom("statistic.work_count", json)));
			
			holder.ratingBar.setRating(StringUtil.isEmpty(BaseTools.getFrom("score", json))?0:Float.parseFloat(BaseTools.getFrom("score", json)));
			if(StringUtil.isEmpty(BaseTools.getFrom("awesome.isAwesome", json))||Integer.parseInt(BaseTools.getFrom("awesome.isAwesome", json))==0){
				holder.zan.setVisibility(View.GONE);
			}else{
				holder.zan.setVisibility(View.VISIBLE);
			}
			if(StringUtil.isEmpty(BaseTools.getFrom("isVerify", json))||Integer.parseInt(BaseTools.getFrom("isVerify", json))==0){
				holder.vip.setVisibility(View.GONE);
			}else{
				holder.vip.setVisibility(View.VISIBLE);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}
}
